package com.xoa.util.guideGoal;

import com.xoa.model.guideGoal.GuideGoalType;
import com.xoa.util.treeUtil.CheckTree;
import com.xoa.util.treeUtil.TreeGirdNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 
 * @ClassName (类名):  FileSortTreeUtil
 * @Description(简述): 生成树结构工具类
 * @author(作者):      ys
 * @date(日期):        2017-4-17 下午4:32:41
 *
 */
public class GuideGoalTreeUtil {
	private final static String MENU_ID = "";



	List<GuideGoalType> rootTree;
	List<GuideGoalType> childTree;
	Map<String,Object>  map;
 	public GuideGoalTreeUtil(List<GuideGoalType> rootTree, List<GuideGoalType> childTree, Map<String,Object>  map){
		this.rootTree = rootTree;
		this.childTree = childTree;
		this.map = map;
	}  
	
	
	public List<TreeGirdNode> getTreeGirdNode(){
		return getRootNodes();
	}
	
	public List<TreeGirdNode> getTreeGirdNodeMap(){
		return getRootNodeMap();
	}
	
	
	
     /**
      * @author 杨  胜
      * @return  List<TreeGirdNode>
      */
	private List<TreeGirdNode>  getRootNodes(){
		List<TreeGirdNode> rootNodes = new ArrayList<TreeGirdNode>();
		for(GuideGoalType fs : rootTree){
			TreeGirdNode node = RootToNode(fs);
			if(node != null){
				addChlidNodes(node);
				rootNodes.add(node);
			}
		}
		return rootNodes;
	}
	 /**
     * @author 杨  胜
     * @return  List<TreeGirdNode>
     */
	public List<TreeGirdNode>  getRootNodeMap(){
		List<TreeGirdNode> rootNodes = new ArrayList<TreeGirdNode>();
		for(GuideGoalType fs : rootTree){
			TreeGirdNode node = RootToNode(fs);
			if(node != null&&map!=null){
				if(CheckTree.checkAll(fs.getUserId(), map)){
				  addChlidNodeMap(node,false);
				}
				rootNodes.add(node);
			}
		}
		return rootNodes;
	}
	
	
	/**
	 * @author 杨  胜
	 * @param menu
	 * @return
	 */
	private TreeGirdNode RootToNode(GuideGoalType fs){
		if(fs == null){
			return null;
		}
		TreeGirdNode node = new TreeGirdNode();
		Map<String,Object> attributes=new HashMap<String, Object>();
		//"|"转义字符串  所以必须用\\进行转义 因为 而且取出数组长度不确定
		   String[] checkStrings=(fs.getUserName()==null?"":fs.getUserName()).split("\\|");
		     //数组长度为1时 说明此时角色和用户Id权限为空
		   if(checkStrings.length==1){
			   node.setDeptNames(checkStrings[0]);
				node.setRoleNames("");
				node.setUserNames("");
		   }
		     //数组长度为2时 说明此时角色和用户Id权限为空
		   if(checkStrings.length==2){
			   node.setDeptNames(checkStrings[0]);
				node.setRoleNames(checkStrings[1]);
				node.setUserNames("");
		   }
		     //数组长度为3时 说明此时部门、角色和用户Id权限都不为空
		   if(checkStrings.length==3){
			   node.setDeptNames(checkStrings[0]);
				node.setRoleNames(checkStrings[1]);
				node.setUserNames(checkStrings[2]);
		   }
		node.setId(String.valueOf(fs.getSid()));
		node.setText(fs.getTypeName());
		return node;
	}
	
	/**
	 * @author 杨  胜       递归生成多层菜单
	 * @param rootNode
	 * @return
	 */
	private void addChlidNodeMap(TreeGirdNode rootNode,boolean flag){
		List<TreeGirdNode> childNodes = new ArrayList<TreeGirdNode>();
		boolean cFlag=false;
		for(GuideGoalType fs: childTree){
			if(rootNode.getId().equals(String.valueOf(fs.getParentId()))){
				TreeGirdNode node = RootToNode(fs);
				childNodes.add(node);
				addChlidNodeMap(node,cFlag);//如果本层找到子节点，则继续遍历下一子节点
			}
		}
		rootNode.setChildren(childNodes);
	}
	/**
	 * @author 杨  胜       递归生成多层菜单
	 * @param rootNode
	 * @return
	 */
	private void addChlidNodes(TreeGirdNode rootNode){
		List<TreeGirdNode> childNodes = new ArrayList<TreeGirdNode>();  
		for(GuideGoalType fs: childTree){
			if(rootNode.getId().equals(String.valueOf(fs.getParentId()))&&CheckTree.checkAll(fs.getUserId(), map)){
				TreeGirdNode node = RootToNode(fs);
				childNodes.add(node);
				addChlidNodes(node);//如果本层找到子节点，则继续遍历下一子节点
			}
		}
		rootNode.setChildren(childNodes);
	}
}
